home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / jwpsrc.zip / CLPBOARD.C < prev    next >
C/C++ Source or Header  |  1993-03-31  |  28KB  |  1,061 lines

  1. /* Copyright (C) Stephen Chung, 1991-1993.  All rights reserved. */
  2.  
  3. #include "jwp.h"
  4.  
  5.  
  6. static BOOL ExternalDestroy = TRUE;
  7.  
  8. static FILEOPTIONS *clipboard = NULL;
  9. static PARAGRAPH far *curpara;
  10.  
  11. static int OriginalLineLength;
  12. static KANJI LastDataKanji;
  13. static int CBPos;
  14. static BOOL CurrentlyInKanji;
  15. static long int CurrentBlockSize;
  16. static BYTE far *DataPtr;
  17. static int DataPos;
  18.  
  19. static FILEPARAHEADER far *fpara = NULL;
  20. static BOOL FormatFirstLastLine = FALSE;
  21. static int NumberOfParagraphs = 0;
  22.  
  23.  
  24. /* Variables used for JWP --> Clipboard */
  25.  
  26. static HANDLE DataHandle;         /* Handle of DataPtr */
  27.  
  28.  
  29. /* Variables used for Clipboard --> JWP */
  30.  
  31. static KANJI far *InsBuffer;        /* Block to hold the insertion buffer */
  32. static int InsPos;
  33.  
  34.  
  35.  
  36. void BlockDelete (FILEOPTIONS *f, POSITION start, POSITION stop)
  37. {
  38.     int len;
  39.     POSITION p;
  40.  
  41.     if (PARAOF(start) == PARAOF(stop)) {
  42.         DeleteString (f, start, POS2ABS(stop) - POS2ABS(start) + 1,
  43.             OP_REFORMAT | OP_UPDATE | OP_MOVESEL | OP_MINIMAL);
  44.         return;
  45.     }
  46.  
  47.     p = start;
  48.  
  49.     /* First delete the last portions of the first paragraph */
  50.  
  51.     if (POSOF(p) > 0) {
  52.         len = unitlen(&UNITOF(p, POSOF(p)));
  53.         DeleteString (f, p, len, 0);
  54.  
  55.         CHAROF(p, POSOF(p)) = '\b';         /* Join the next paragraph */
  56.         CHAROF(p, POSOF(p) + 1) = 0;
  57.         f->nr_bytes++;
  58.  
  59.         PARAOF(p) = PARAOF(p)->next;
  60.         LINEOF(p) = PARAOF(p)->lines;
  61.         POSOF(p) = 0;
  62.     }
  63.  
  64.     /* Delete the middle paragraphs */
  65.  
  66.     for (; PARAOF(p) != PARAOF(stop); ) {
  67.         f->nr_bytes -= (unitlen(PARAOF(p)->text) - 1);
  68.         CHAROF(p, 0) = '\b';
  69.         CHAROF(p, 1) = 0;
  70.  
  71.         PARAOF(p) = PARAOF(p)->next;
  72.         LINEOF(p) = PARAOF(p)->lines;
  73.         POSOF(p) = 0;
  74.     }
  75.  
  76.     /* Finally we delete the first portion of the last paragraph */
  77.     /* and update the whole thing                                */
  78.  
  79.     len = POS2ABS(stop);
  80.  
  81.     if (len >= 0) {
  82.         if (CHAROF(stop,POSOF(stop))) len++;
  83.  
  84.         DeleteString (f, p, len, 0);
  85.     }
  86.  
  87.     /* Set the cursor position */
  88.  
  89.     f->current = p;
  90.  
  91.     ReformatParagraph (f, start, PARAOF(start)->next, OP_REFORMAT | OP_UPDATE | OP_MOVESEL | OP_MINIMAL);
  92. }
  93.  
  94.  
  95. void BlockReplace (FILEOPTIONS *f, POSITION start, POSITION stop, KANJI far *buf)
  96. {
  97.     int len;
  98.     POSITION p;
  99.  
  100.     if (PARAOF(start) == PARAOF(stop)) {
  101.         ReplaceString (f, start, POS2ABS(stop) - POS2ABS(start) + 1, buf,
  102.             OP_REFORMAT | OP_UPDATE | OP_MOVESEL | OP_MINIMAL);
  103.         return;
  104.     }
  105.  
  106.     p = start;
  107.  
  108.     /* First replace the last portions of the first paragraph */
  109.  
  110.     len = unitlen(&UNITOF(p, POSOF(p)));
  111.     ReplaceString (f, p, len, buf, 0);
  112.     len = unitlen(PARAOF(p)->text);
  113.  
  114.     CHAROF(p, len) = '\b';         /* Join the next paragraph */
  115.     CHAROF(p, len + 1) = 0;
  116.     f->nr_bytes++;
  117.  
  118.     PARAOF(p) = PARAOF(p)->next;
  119.     LINEOF(p) = PARAOF(p)->lines;
  120.     POSOF(p) = 0;
  121.  
  122.     /* Delete the middle paragraphs */
  123.  
  124.     for (; PARAOF(p) != PARAOF(stop); ) {
  125.         f->nr_bytes -= (unitlen(PARAOF(p)->text) - 1);
  126.         CHAROF(p, 0) = '\b';
  127.         CHAROF(p, 1) = 0;
  128.  
  129.         PARAOF(p) = PARAOF(p)->next;
  130.         LINEOF(p) = PARAOF(p)->lines;
  131.         POSOF(p) = 0;
  132.     }
  133.  
  134.     /* Finally we delete the first portion of the last paragraph */
  135.     /* and update the whole thing                                */
  136.  
  137.     len = POS2ABS(stop);
  138.  
  139.     if (CHAROF(stop,POSOF(stop))) len++;
  140.  
  141.     DeleteString (f, p, len, 0);
  142.  
  143.     f->current = p;                /* Set the cursor position */
  144.  
  145.     ReformatParagraph (f, start, PARAOF(start)->next, OP_REFORMAT | OP_UPDATE | OP_MOVESEL | OP_MINIMAL);
  146. }
  147.  
  148.  
  149.  
  150. static long int HowMuchData (void)
  151. {
  152.     int i;
  153.     long int len;
  154.     PARAGRAPH far *p;
  155.  
  156.     /* How much data? */
  157.  
  158.     for (p = clipboard->paragraph, len = 0L; p != NULL; p = p->next) {
  159.         for (i = 0; p->text[i].kanji; i++) len += (ISKANJI(p->text[i].kanji) ? 2 : 1);
  160.         len += 1L;   /* for \n */
  161.     }
  162.  
  163.     return (len);
  164. }
  165.  
  166.  
  167.  
  168. static HANDLE PrepareEUCData (void)
  169. {
  170.     int i;
  171.     long int len;
  172.     HANDLE handle;
  173.     PARAGRAPH far *p;
  174.     BYTE far *cp;
  175.  
  176.     if (clipboard == NULL) {
  177.         ErrorMessage(global.hwnd, "Funny! Clipboard is empty!");
  178.         return (NULL);
  179.     }
  180.  
  181.  
  182.     /* Allocate the data block */
  183.  
  184.     len = HowMuchData() + 5L;
  185.  
  186.     if (len > C64K) {
  187.         ErrorMessage(global.hwnd, "The clipboard cannot hold so much data!  The limit is 64K!");
  188.         return (NULL);
  189.     }
  190.  
  191.     handle = GlobalAlloc(GHND, len);
  192.     cp = (BYTE far *) GlobalLock(handle);
  193.  
  194.     /* Copy data */
  195.  
  196.     for (p = clipboard->paragraph; p != NULL; p = p->next) {
  197.         for (i = 0; p->text[i].kanji; i++, cp++) {
  198.             if (ISKANJI(p->text[i].kanji)) {
  199.                 *cp++ = HIBYTE(p->text[i].kanji) | 0x80;
  200.                 *cp = LOBYTE(p->text[i].kanji) | 0x80;
  201.             } else {
  202.                 *cp = LOBYTE(p->text[i].kanji) & 0x7f;
  203.             }
  204.         }
  205.         *cp++ = '\n';
  206.     }
  207.     *cp++ = '\0';
  208.  
  209.     GlobalUnlock(handle);
  210.     return (handle);
  211. }
  212.  
  213.  
  214.  
  215. static int ToClipboardCharIn (void)
  216. {
  217.     KANJI ch;
  218.  
  219.     if (curpara == NULL) return (EOF);
  220.  
  221.     ch = curpara->text[CBPos].kanji;
  222.  
  223.     if (CurrentlyInKanji) {
  224.         CurrentlyInKanji = FALSE;
  225.         CBPos++;
  226.         return (LOBYTE(ch) | 0x80);
  227.     }
  228.  
  229.     if (!ch) {
  230.         curpara = curpara->next;
  231.         CBPos = 0;
  232.         return ('\n');
  233.     } else if (ISKANJI(ch)) {
  234.         CurrentlyInKanji = TRUE;
  235.         return (HIBYTE(ch) | 0x80);
  236.     } else {
  237.         CBPos++;
  238.         return (LOBYTE(ch) & 0x7f);
  239.     }
  240. }
  241.  
  242.  
  243.  
  244. static void ToClipboardCharOut (int ch)
  245. {
  246.     if (DataPos + 5 >= CurrentBlockSize) {
  247.         /* Reallocate */
  248.         CurrentBlockSize += TEXTBLOCKSIZE;
  249.         if (CurrentBlockSize > C64K) {
  250.             ErrorMessage (global.hwnd, "The clipboard cannot hold so much data!  The limit is 64K!");
  251.             return;
  252.         }
  253.         GlobalUnlock(DataHandle);
  254.         GlobalReAlloc(DataHandle, CurrentBlockSize, GHND);
  255.         DataPtr = (BYTE far *) GlobalLock(DataHandle);
  256.     }
  257.  
  258.     DataPtr[DataPos++] = ch;
  259. }
  260.  
  261.  
  262.  
  263. static int FromClipboardCharIn (void)
  264. {
  265.     KANJI ch;
  266.  
  267.     ch = DataPtr[DataPos++];
  268.     if (!ch) return (EOF);
  269.  
  270.     return (ch);
  271. }
  272.  
  273.  
  274.  
  275. static void FromClipboardCharOut (int ch)
  276. {
  277.     KANJI ch1;
  278.  
  279.     if (ch == '\r') return;
  280.     if (ch == '\n') ch = '\r';
  281.  
  282.     if (CurrentlyInKanji) {
  283.         CurrentlyInKanji = FALSE;
  284.         ch1 = ((LastDataKanji << 8) | (ch & 0x7f));
  285.     } else if (ch & 0x80) {
  286.         CurrentlyInKanji = TRUE;
  287.         LastDataKanji = (ch & 0x7f);
  288.         return;
  289.     } else {
  290.         ch1 = ch;
  291.     }
  292.  
  293.     if (InsPos + 5 >= CurrentBlockSize) {
  294.         /* Reallocate */
  295.         CurrentBlockSize += TEXTBLOCKSIZE;
  296.         if (CurrentBlockSize > (C64K / sizeof(KANJI))) {
  297.             ErrorMessage (global.hwnd, "The clipboard contains too much data!  The limit is 64K!");
  298.             return;
  299.         }
  300.         InsBuffer = BlockRealloc(InsBuffer, CurrentBlockSize * sizeof(KANJI));
  301.     }
  302.  
  303.     InsBuffer[InsPos++] = ch1;
  304. }
  305.  
  306.  
  307.  
  308. static HANDLE PrepareOtherData (FILEFORMAT format)
  309. {
  310.     int i;
  311.     long int len;
  312.  
  313.     if (clipboard == NULL) {
  314.         ErrorMessage(global.hwnd, "Funny! Clipboard is empty!");
  315.         return (NULL);
  316.     }
  317.  
  318.     curpara = clipboard->paragraph;
  319.     CBPos = 0;
  320.  
  321.     len = HowMuchData() + 5L;
  322.  
  323.     if (len > C64K) {
  324.         ErrorMessage (global.hwnd, "The clipboard cannot hold so much data!  The limit is 64K!");
  325.         return (NULL);
  326.     }
  327.  
  328.     i = len / TEXTBLOCKSIZE;
  329.     len = (i + 1) * TEXTBLOCKSIZE;
  330.  
  331.     DataHandle = GlobalAlloc(GHND, len);
  332.     DataPtr = (BYTE far *) GlobalLock(DataHandle);
  333.  
  334.     CurrentBlockSize = len;
  335.     DataPos = 0L;
  336.  
  337.     CurrentlyInKanji = FALSE;
  338.  
  339.     SetupIO(ToClipboardCharIn, ToClipboardCharOut);
  340.     FileExport(format);
  341.  
  342.     GlobalUnlock(DataHandle);
  343.     return (DataHandle);
  344. }
  345.  
  346.  
  347.  
  348. static HANDLE PrepareBitmap (void)
  349. {
  350.     long int height, width, max;
  351.     HBITMAP hbitmap;
  352.     HDC hdc, hdcmem;
  353.     HBRUSH hbrush;
  354.     RECT rect;
  355.     PARAGRAPH far *p;
  356.     ONELINE far *lp;
  357.     BOOL OldDraftView;
  358.  
  359.